package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.InboundItems;
import com.ruoyi.system.mapper.InboundsMapper;
import com.ruoyi.system.domain.Inbounds;
import com.ruoyi.system.service.IInboundsService;

/**
 * 入库单Service业务层处理
 * 
 * @author he
 * @date 2025-09-18
 */
@Service
public class InboundsServiceImpl implements IInboundsService 
{
    @Autowired
    private InboundsMapper inboundsMapper;

    /**
     * 查询入库单
     * 
     * @param id 入库单主键
     * @return 入库单
     */
    @Override
    public Inbounds selectInboundsById(Long id)
    {
        return inboundsMapper.selectInboundsById(id);
    }

    /**
     * 查询入库单列表
     * 
     * @param inbounds 入库单
     * @return 入库单
     */
    @Override
    public List<Inbounds> selectInboundsList(Inbounds inbounds)
    {
        return inboundsMapper.selectInboundsList(inbounds);
    }

    /**
     * 新增入库单
     * 
     * @param inbounds 入库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInbounds(Inbounds inbounds)
    {
        inbounds.setCreateTime(DateUtils.getNowDate());
        inbounds.setCreateUserId(SecurityUtils.getUserId());
        inbounds.setCreateUserName(SecurityUtils.getUsername());
        int rows = inboundsMapper.insertInbounds(inbounds);
        insertInboundItems(inbounds);
        return rows;
    }

    /**
     * 修改入库单
     * 
     * @param inbounds 入库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInbounds(Inbounds inbounds)
    {
        inbounds.setUpdateTime(DateUtils.getNowDate());
    inbounds.setUpdateUserId(SecurityUtils.getUserId());
    inbounds.setUpdateUserName(SecurityUtils.getUsername());
        inboundsMapper.deleteInboundItemsByInterId(inbounds.getId());
        insertInboundItems(inbounds);
        return inboundsMapper.updateInbounds(inbounds);
    }

    /**
     * 批量删除入库单
     * 
     * @param ids 需要删除的入库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInboundsByIds(Long[] ids)
    {
        inboundsMapper.deleteInboundItemsByInterIds(ids);
        return inboundsMapper.deleteInboundsByIds(ids);
    }

    /**
     * 删除入库单信息
     * 
     * @param id 入库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInboundsById(Long id)
    {
        inboundsMapper.deleteInboundItemsByInterId(id);
        return inboundsMapper.deleteInboundsById(id);
    }

    /**
     * 新增入库单项信息
     * 
     * @param inbounds 入库单对象
     */
    public void insertInboundItems(Inbounds inbounds)
    {
        List<InboundItems> inboundItemsList = inbounds.getInboundItemsList();
        Long id = inbounds.getId();
        if (StringUtils.isNotNull(inboundItemsList))
        {
            List<InboundItems> list = new ArrayList<InboundItems>();
            for (InboundItems inboundItems : inboundItemsList)
            {
                inboundItems.setInterId(id);
                list.add(inboundItems);
            }
            if (list.size() > 0)
            {
                inboundsMapper.batchInboundItems(list);
            }
        }
    }
}
